<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11 0011
 * Time: 下午 4:36
 */
namespace app\core\controller;
use app\admin\controller\Admin;
use think\Db;
class Product extends Admin{
    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '关于我们管理',
                'description' => '管理关于我们',
            ),
            'menu' => array(
                array(
                    'name' => '产品分类列表',
                    'url' => url('classifyList'),
                ),
                array(
                    'name' => '应用领域列表',
                    'url' => url('scopeList'),
                ),
                array(
                    'name' => '产品列表',
                    'url' => url('productList'),
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加产品',
                    'url' => url('productAdd'),
                ),
                array(
                    'name' => '添加应用领域',
                    'url' => url('scopeAdd'),
                ),
                array(
                    'name' => '添加产品分类',
                    'url' => url('classifyAdd'),
                ),

            ),
        );
    }

    //产品分类列表
    public function classifyList(){
        $list = Db::name('core_product_classify')->where($this->classify_condition(input()))->paginate(10);
        $this->view->list = $list->all();
        $this->view->page = $list->render();
        return $this->fetch();
    }

    //产品分类添加、修改
    public function classifyAdd(){
        if($this->request->isPost()){
            $data = input('post.');
            if($data['id']){
                $content = '产品分类修改';$status = 2;
                Db::name('core_product_classify')->where('id',input('id'))->update($data);
            }else{
                $content = '产品分类添加';$status = 1;
                if(Db::name('core_product_classify')->where('classify_name',$data['classify_name'])->find()){
                    return ajaxReturn(500,'产品分类名称存在');
                }
                $data['create_time'] = time();
                Db::name('core_product_classify')->insert($data);
            }
            $this->log($content,$status);
            return ajaxReturn(200,'操作成功',url('classifyList'));
        }
        $this->view->info = Db::name('core_product_classify')->where('id',input('id'))->find();
        return $this->fetch();
    }

    //产品分类删除
    public function classifyDel(){
        $content = '产品分类删除';$status = 3;
        if(Db::name('core_product')->where('classify_id',input('id'))->find()){
            return ajaxReturn(500,'当前产品分类下存在产品，请删除后操作');
        }else{
            Db::name('core_product_classify')->delete(input('id'));
        }
        $this->log($content,$status);
        return ajaxReturn(200,'删除成功');
    }

    //应用领域列表
    public function scopeList(){
        $list = Db::name('core_product_scope')->where($this->scope_condition(input()))->paginate(10);
        $this->view->list = $list->all();
        $this->view->page = $list->render();
        return $this->fetch();
    }

    //应用领域添加、修改
    public function scopeAdd(){
        if($this->request->isPost()){
            $data = input('post.');
            unset($data['image']);
            if($data['id']){
                $content = '应用领域修改';$status = 2;
                Db::name('core_product_scope')->where('id',input('id'))->update($data);
            }else{
                $content = '应用领域添加';$status = 1;
                if(Db::name('core_product_scope')->where('scope_name',$data['scope_name'])->find()){
                    return ajaxReturn(500,'应用领域名称存在');
                }
                $data['create_time'] = time();
                Db::name('core_product_scope')->insert($data);
            }
            $this->log($content,$status);
            return ajaxReturn(200,'操作成功',url('scopeList'));
        }
        $this->view->info = Db::name('core_product_scope')->where('id',input('id'))->find();
        return $this->fetch();
    }

    //应用领域删除
    public function scopeDel(){
        $content = '应用领域删除';$status = 3;
        if(Db::name('core_product')->where('scope_id',input('id'))->find()){
            return ajaxReturn(500,'当前应用领域下存在产品，请删除后操作');
        }else{
            Db::name('core_product_scope')->delete(input('id'));
        }
        $this->log($content,$status);
        return ajaxReturn(200,'删除成功');
    }

    //产品列表
    public function productList(){
        $list = Db::name('core_product')
            ->alias('a')
            ->field('a.*')
            ->view('core_product_classify b','classify_name','a.classify_id=b.id','LEFT')
            ->view('core_product_scope c','scope_name','a.scope_id=c.id','LEFT')
            ->where($this->product_condition(input()))
            ->order('create_time desc')
            ->paginate(10);
        $this->view->list = $list->all();
        $this->view->page = $list->render();
        return $this->fetch();
    }

    //产品添加、修改
    public function productAdd(){
        if($this->request->isPost()){
            $data = input('post.');
            unset($data['image']);
            unset($data['file']);
            if(isset($data['pics'])) $data['pics'] = serialize(array_values($data['pics']));
            if(isset($data['product_param'])) $data['product_param'] = serialize(array_values($data['product_param']));
            if($data['id']){
                $content = '产品修改';$status = 2;
                Db::name('core_product')->where('id',input('id'))->update($data);
            }else{
                $content = '产品添加';$status = 1;
                if(Db::name('core_product')->where('product_name',$data['product_name'])->find()){
                    return ajaxReturn(500,'产品名称存在');
                }
                $data['create_time'] = time();
                Db::name('core_product')->insert($data);
            }
            $this->log($content,$status);
            return ajaxReturn(200,'操作成功',url('productList'));
        }
        $info = Db::name('core_product')->where('id',input('id'))->find();
        if($info){
            $info['pics'] = unserialize($info['pics']);
            $this->view->start_act = count($info['product_param'])+1;
            $info['product_param'] = unserialize($info['product_param']);
        }
//        dump($info);die;
        $this->view->info = $info;
        $this->view->classifyInfo = Db::name('core_product_classify')->where('status',1)->select();
        $this->view->scopeInfo = Db::name('core_product_scope')->where('status',1)->select();
        return $this->fetch();
    }

    //产品删除
    public function productDel(){
        $content = '产品删除';$status = 3;
        Db::name('core_product')->delete(input('id'));
        $this->log($content,$status);
        return ajaxReturn(200,'删除成功');
    }

    //移除展示图
    public function picsDel(){
        $pics = unserialize(Db::name('core_product')->where('id',input('id'))->value('pics'));
        foreach($pics as $k =>$v){
            if(input('key') == $k){
                $url = '../public'.$v;
                if(file_exists($url)){
                    unlink($url);
                    unset($pics[$k]);
                }else{
                    return json(array('msg'=>'文件不存在'));
                }
            }
        }
        $data['pics'] = serialize(array_values($pics));
        Db::name('core_product')->where('id',input('id'))->update($data);
        return json(200);
    }

    //新品推荐
    public function xp_tj(){
            if($this->request->isPost()){
                $data = input('post.');
                $idstr =$data['ids'];
                $ids = explode(',',$idstr);
                try{
                    foreach ($ids as $v){
                        Db::name('core_product')->where('id',$v)->setField('is_tj',1);
                    }
                    Db::commit();
                }catch(\Exception $e){
                    Db::rollback();
                    die($e->getMessage());
                }
                return json(200);
            }
    }

    //取消推荐
    public function qx_tj(){
        if($this->request->isPost()){
            $data = input('post.');
            $idstr =$data['ids'];
            $ids = explode(',',$idstr);
            try{
                foreach ($ids as $v){
                    Db::name('core_product')->where('id',$v)->setField('is_tj',2);
                }
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
                die($e->getMessage());
            }
            return json(200);
        }
    }

    /**
     * 索引条件
     * @param   $data   提交数据
     */
    protected function classify_condition($data){
        $where = '';
        if(!empty($data['classify_name'])){
            $where['classify_name'] = array('like','%'.$data['classify_name'].'%');
        }
        if(!empty($data['status'])){
            $where['status'] = $data['status'];
        }
        return $where;
    }

    protected function scope_condition($data){
        $where = '';
        if(!empty($data['scope_name'])){
            $where['scope_name'] = array('like','%'.$data['scope_name'].'%');
        }
        if(!empty($data['status'])){
            $where['status'] = $data['status'];
        }
        return $where;
    }

    protected function product_condition($data){
        $where = '';
        if(!empty($data['product_name'])){
            $where['a.product_name'] = array('like','%'.$data['product_name'].'%');
        }
        if(!empty($data['status'])){
            $where['a.status'] = $data['status'];
        }
        return $where;
    }
}